package Gestores;

import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DataBaseLink {

    // Datos para la conexcion con una base de datos.
    private String usuario, password, baseDatos, url, paquete, ip, port;
    //Managers.
    private Connection connection;
    // Banderas.
    private boolean state;
    public static final boolean OPEN = true, CLOSE = false;
    private Statement statement ;
    private ResultSet resultado ;

    public DataBaseLink() throws ClassNotFoundException {
        paquete = Driver.class.getName();// org.postgresql.Driver
        Class.forName(paquete);

        /* Datos de protocolo (Datos standar) */
        url = "jdbc:mysql://";
        ip = null;
        port = null;
        baseDatos = null;

        /* Datos de un usuario */
        usuario = null;
        password = null;
    }

    public boolean startConnection() throws SQLException {
        String msg = "";
        if (ip == null) {
            msg += "Falta una Ip.\n";
        }
        if (port == null) {
            msg += "Falta un Puerto.\n";
        }
        if (baseDatos == null) {
            msg += "Falta una Base de datos.\n";
        }
        if (usuario == null) {
            msg += "Falta un Usuario.\n";
        }

        if ((ip != null) && (port != null) && (baseDatos != null) && (usuario != null)) {
            url += ip + ':' + port + '/' + baseDatos;
            connection = DriverManager.getConnection(url, usuario, password);
            state = OPEN;
            System.out.println("Base de Datos conectada.");
            return true;
        } else {
            System.err.println("No se pudo conectar con la base de datos.");
            throw new SQLException(msg);
        }
    }

    public boolean terminatedConnection() throws SQLException {
        if (connection == null) {
            state = CLOSE;
            return true;
        } else {
            connection.close();
            connection = null;
            state = CLOSE;
            return true;
        }
    }

    public ResultSet executeSql(String sentence) throws Exception {
        if (state == OPEN) {
            statement = connection.createStatement();
            resultado = statement.executeQuery(sentence);
          //  statement.close();
            return resultado;
        } else {
            throw new Exception("Couldn't stablish connection");
        }
    }
    public void CerrarStatement() throws SQLException{
            this.statement.close();
    }
    public void setIP(String ip) {
        this.ip = ip;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public void setBaseDatos(String basedatos) {
        this.baseDatos = basedatos;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public void setContrasena(String contrasena) {
        this.password = contrasena;
    }

    public String getIp() {
        return ip;
    }

    public String getport() {
        return port;
    }

    public String getBaseDatos() {
        return baseDatos;
    }

    public String getUsuario() {
        return usuario;
    }

    public String getContrasena() {
        return password;
    }
}
